package com.sinosun.autotestmachine.tools.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import android.text.TextUtils;
import com.eyibc.EYIBCException;
import com.eyibc.EYIBCLib;
import com.sinosun.autotestmachine.App;
import com.sinosun.autotestmachine.tools.parser.ContentMaker;
import com.sinosun.mstplib.SafeMode;
import com.sinosun.mstplib.util.Utils;


/**
 * 签字实现类
 * @author HYW
 *
 */
public class SignDataFunc {
    // 私钥句柄
    static long privateCtx = 0;
    static EYIBCLib EYIBCInstance;
    // 引擎句柄
    static long LibContext = 0;
    static String tokenKeyFileName = App.getTestConfig().getTokenKeyFileName();
    static String tokenPassword = App.getTestConfig().getTokenPassword();
    static String tokenKey = App.getTestConfig().getTokenKey();
//    static String sm2PrivateKey = App.getTestConfig().getSm2PrivateKey();
//    static String sm2PrivateKeypwd = App.getTestConfig().getSm2PrivateKeypwd();
    static long ibcParamCtx = 0; // IBC参数句柄
	//获得授权
    public static String getAuthInfo(String userId, long expireTime, long creationTime, int asymmetricCryptoType) {
        String ret = "";
        try {
            // 用app的私钥对userId + expireTime进行签名
            String sNeedSigner = userId + "@" + String.valueOf(expireTime) + "@" + String.valueOf(creationTime);
            byte[] signer = cryptSignData(sNeedSigner);
            ret = Utils.base64Encode(signer);
            
            if(asymmetricCryptoType == SafeMode.AsymmetricCryptoType.ALG_IBC)
            {
            	signer = cryptSignData(sNeedSigner);
            }
            else if (asymmetricCryptoType == SafeMode.AsymmetricCryptoType.ALG_SM2)
            {
            	signer = cryptSM2SignData(sNeedSigner.getBytes("UTF-8"));
            }
            else
            {
            	signer = cryptSignData(sNeedSigner);
            }
            ret = com.sinosun.mstplib.util.Utils.base64Encode(signer);
        } catch (EYIBCException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ret;
    }
    
    /**
     * sm2数据签名
     */
    public static byte[] cryptSM2SignData(byte[] srcData) throws EYIBCException {
    	
//    	//创建引擎句柄
//    	EYIBCInstance = EYIBCLib.getInstance();
//    	LibContext = EYIBCInstance.createLibCtx();
//    	
//       	cpAssertToFiles(tokenKey);
//        if (!EYIBCLib.isIBCError(LibContext)) {
//            String ibcParaPath = App.getInstance().getFilesDir().getPath() + File.separator + tokenKey;
//            ibcParamCtx = EYIBCInstance.loadIBCParamIntoLibCtx(LibContext, ibcParaPath);
//        }
//    	
//    	long hashObject = EYIBCInstance.genHashObject(srcData, EYIBCAlg.ALG_HASH_SM3);
//		byte[] prvKeyBuf = Utils.base64Decode(sm2PrivateKey);
//		long prvkeyObject = EYIBCInstance.createSM2PrvKeyObjectFromMem(LibContext, prvKeyBuf, prvKeyBuf.length, sm2PrivateKeypwd, 0);        
//		return EYIBCInstance.cryptSignDataEx(prvkeyObject, hashObject);
    	return null;
    }
    
    
    //数据签名
    public static byte[] cryptSignData(String srcData) throws EYIBCException {
    	//创建引擎句柄
    	EYIBCInstance = EYIBCLib.getInstance();
    	LibContext = EYIBCInstance.createLibCtx();
    	
    	
    	//载入参数
    	ContentMaker.cpAssertToFiles4Other(tokenKey);
        if (!EYIBCLib.isIBCError(LibContext)) {
            String ibcParaPath = App.getInstance().getFilesDir().getPath() + File.separator + tokenKey;
            ibcParamCtx = EYIBCInstance.loadIBCParamIntoLibCtx(LibContext, ibcParaPath);
        }
    	
    	//应用私钥
    	String path = App.getInstance().getFilesDir().getPath() + File.separator + tokenKeyFileName;
        String privatePassword = tokenPassword;
        
        ContentMaker.cpAssertToFiles4Other(tokenKeyFileName);
    	privateCtx = EYIBCInstance.createPrvKeyObjectFromFile(LibContext, path, privatePassword);
        if (TextUtils.isEmpty(srcData) || (EYIBCLib.isIBCError(privateCtx))) {
            return null;
        }
        return EYIBCInstance.cryptSignData(privateCtx, srcData.getBytes());
    }
    
    
}
